 var geoReasons=[];
 geoReasons[G_GEO_SUCCESS]            = "Success";
 geoReasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
 geoReasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
 geoReasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual geoReasons.";
 geoReasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
 geoReasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";                                      
 geoReasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";




/* start the search menu Object ************************************************************* */
function SearchControl() {

}
SearchControl.prototype = new GControl();

SearchControl.prototype.initialize = function(map) {
	var geocoder = new GClientGeocoder();





  var container = document.createElement("div");

	var searchDiv = document.createElement("div");
	this.searchBox = document.createElement("input");
	var searchBtn = document.createElement("input");
	var nobr = document.createElement("nobr");

	this.setButtonStyle_(searchDiv);

	this.searchBox.type="text";

	searchBtn.type="button";
	searchBtn.value="Search";

	nobr.appendChild(this.searchBox);
	nobr.appendChild(document.createTextNode(" "));	
	nobr.appendChild(searchBtn);	

	searchDiv.appendChild(nobr);	
	
	container.appendChild(searchDiv);
	
	var doSearch = function(event){
		var hope = this.parentNode.firstChild;
		var node = event.target;
		var type = event.type;
		var ok = false;
		if(type == "click"){
			ok = true;
		}else if(type == "keypress"){
			var key = event.keyCode;
			var ch = event.charCode;
			if(key == 13){
				ok = true;				
			}
		}
		if (ok ) {
		geocoder.getLocations(hope.value, function (result)
          {
            if (result.Status.code == G_GEO_SUCCESS) {
              if (result.Placemark.length>1) {
	              GLog.write("Found " +result.Placemark.length +" results");
	              for (var i=0; i<result.Placemark.length; i++) {
	                var p = result.Placemark[i].Point.coordinates;
	                GLog.write((i+1)+": "+ result.Placemark[i].address + marker.getPoint());
	              }
	              var p = result.Placemark[0].Point.coordinates;
	              map.setCenter(new GLatLng(p[1],p[0]),14);
              } else {
	                var p = result.Placemark[0].Point.coordinates;   
	              map.setCenter(new GLatLng(p[1],p[0]),14);
              }

            }
            // ====== Decode the error status ======
            else {
              var reason="Code "+result.Status.code;
              if (geoReasons[result.Status.code]) {
                reason = geoReasons[result.Status.code]
              } 
              alert('Could not find "'+hope.value+ '" ' + reason);
            }
          });}
          /*
			geocoder.getLatLng(hope.value, function (point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.panTo(point);
				}
			});*/
		};
	
	GEvent.addDomListener(this.searchBox, "keypress",doSearch );
	GEvent.addDomListener(this.searchBox, "focus",function(){this.value="";} );
	GEvent.addDomListener(searchBtn, "click",doSearch );
  
  map.getContainer().appendChild(container);
  return container;
};

SearchControl.prototype.getSearchButtonHandler = function(instance) {
	return function(){
		instance.fn(instance.searchBox.value);
	};
};

SearchControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(7, 7));
};

SearchControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "none";
  /*button.style.color = "#0000cc";*/
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "17em";
  button.style.cursor = "pointer";
};
/* end search controls*/
